import 'package:cbec_app/components/common/brand_item.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:video_player/video_player.dart';

import '../../../components/base/base_ctr.dart';
import '../../../components/http/network.dart';
import '../../../utils/AppData.dart';
import '../../../utils/Tools.dart';

class CommentListCtr extends BaseController with SingleGetTickerProviderMixin {
  VideoPlayerController? videoController;

  late TabController statusTabController;

  ///评论分页
  ///每页数量
  int commentPageSize = 10;

  ///当前页
  int commentPageNum = 1;
  Map? data;

  ///评论列表
  List? commentList;
  String commodityId = '';
  int type = 0;
  int page = 1;
  bool hasNextPage = false;
  // int initTabIndex = 0;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    commodityId = Get.arguments['id'];
    getCommentList(type);
    statusTabController = TabController(length: 5, vsync: this, initialIndex: type);
    statusTabController.addListener(() {
      page = 1;
      if (statusTabController.indexIsChanging) {
        // 当tab切换时执行的操作
        switch (statusTabController.index) {
          // 1-好评 2-中评 3-差评 4-图片/视频 不传-全部
          case 0:
            type = 0;

            break;
          case 1:
            type = 4;
            break;
          case 2:
            type = 1;
            break;
          case 3:
            type = 2;
            break;
          case 4:
            type = 3;
            break;
        }
        getCommentList(type);
      }
    });
  }

  void initVideoC(
    String url,
  ) {
    videoController?.dispose();
    Uri sourceUri = Uri.parse(url);
    videoController = VideoPlayerController.networkUrl(sourceUri);
    videoController?.addListener(() {
      update();
    });
    videoController?.setLooping(false);
    videoController?.initialize().then((_) {
      update();
    });
    videoController?.play();
  }

  ///获取评论列表
  void getCommentList(int type) {
    Tools.showLoading();
    Map<String, dynamic> params = {};
    params['id'] = commodityId;
    params['sid'] = AppData.sid;
    params['pageNum'] = commentPageNum;
    if (type != 0) {
      params['type'] = type.toString();
    }
    params['pageSize'] = commentPageSize;
    Network.getGoodsCommentList(commodityId, params).then((value) {
      data = value;
      if (page == 1) {
        commentList = data?['pageInfo']['list'];
      } else {
        commentList?.addAll(data?['pageInfo']['list']);
      }
      hasNextPage = data?['pageInfo']['hasNextPage'];
      update();
    });
    Tools.hideLoading();
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    page = 1;
    getCommentList(type);
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    if (hasNextPage == true) {
      super.onPageLoading();
      page += 1;
      getCommentList(type);
      baseRefreshCtr.loadComplete();
    }
  }

  @override
  void onClose() {
    statusTabController.dispose();
    videoController?.dispose();
    super.onClose();
  }
}
